package com.beisita.web.controller;

import com.beisita.dao.entity.StuEntity;
import com.beisita.web.controller.common.BaseActionAbstract;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.util.HashMap;
import java.util.Map;

@EnableWebMvc
@RestController
@RequestMapping({"/rest"})
public class ArticleController extends BaseActionAbstract {
    @Value("${name}")
    private String name;

    @Value("${spring.application.name}")
    private String cname;

    @RequestMapping(value = {"/article"}, method = {RequestMethod.POST}, produces = {"application/json"})
    public Model saveArticle(@RequestBody StuEntity stuEntity, Model model) {
//        stuEntity.setId(Long.getLong("1"));
        Map<String, Object> ret = new HashMap<>();
//        ret.put("id", stuEntity.getId());
        model.addAllAttributes(ret);
        return model;
    }

    @RequestMapping(value = {"/article/{id}"}, method = {RequestMethod.DELETE}, produces = {"application/json"})
    public Model deleteArticle(@PathVariable Long id, Model model) {
        return model;
    }

    @RequestMapping(value = {"/article/{id}"}, method = {RequestMethod.PUT}, produces = {"application/json"})
    public Model updateArticle(@PathVariable Long id, @RequestBody StuEntity article, Model model) {
        return model;
    }

    @RequestMapping(value = {"/article/{id}"}, method = {RequestMethod.GET}, produces = {"application/json"})
    public Model getArticle(@PathVariable Long id, Model model) {
        return model;
    }

    @RequestMapping({"/index"})
    public String index(Model model) {
        model.addAttribute("name", "Ryan");
        return "index";
    }

    @GetMapping({"/home"})
    public String html() {
        return "redirect:home.html";
    }

    @RequestMapping({"/"})
    public String hello(Model model) {
        model.addAttribute("name", this.name);
        return "hello";
    }

    @RequestMapping({"/json"})
    @ResponseBody
    public Map<String, Object> json() {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "Ryan");
        map.put("age", "18");
        map.put("sex", "man");
        map.put("customeName", this.name);
        map.put("cName", this.cname);
        return map;
    }
}

